﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccess;
namespace SQT.tracnghiem.Admin
{
    public partial class DefaultAdmin : System.Web.UI.Page
    {
        public int last_id,new_user,count_test,check_date =0,week=0, week_id, week_name, week_year;

        public DateTime work = DateTime.Now,update;
        public DateTime? monday;
        protected void Page_Load(object sender, EventArgs e)
        {
            var get_last = new TblChatController().FetchAll().Last();
            if (get_last == null)
                last_id = 0;
            else
                last_id = get_last.ChatId;

            var get_user = new TblUserController().FetchAll().Where(x => DateTime.Parse(x.SignInDate.ToString()).Day + "/" + DateTime.Parse(x.SignInDate.ToString()).Month + "/" + DateTime.Parse(x.SignInDate.ToString()).Year == DateTime.Parse(DateTime.Now.ToString()).Day + "/" + DateTime.Parse(DateTime.Now.ToString()).Month + "/" + DateTime.Parse(DateTime.Now.ToString()).Year).ToList();
            new_user = get_user.Count;

            var get_test = new TblResultController().FetchAll().Where(x => DateTime.Parse(x.ResultDate.ToString()).Day + "/" + DateTime.Parse(x.ResultDate.ToString()).Month + "/" + DateTime.Parse(x.ResultDate.ToString()).Year == DateTime.Parse(DateTime.Now.ToString()).Day + "/" + DateTime.Parse(DateTime.Now.ToString()).Month + "/" + DateTime.Parse(DateTime.Now.ToString()).Year).ToList();
            count_test = get_test.Count;
            week = WeekOfYear(DateTime.Now);
            //week--;
            monday = MondayOfWeek(week,work.Year);
            var get_work = new TblWeekController().FetchAll().Where(x => DateTime.Parse(x.Monday.ToString()).Date == DateTime.Parse(monday.ToString()).Date).ToList();
            if (get_work.Count != 0)
            {
                check_date = 1;
                update = DateTime.Parse(get_work[0].UpdateDate.ToString());
                week_id = get_work[0].Id;
                week_name = (int)get_work[0].Week;
                week_year = (int)get_work[0].Year;
            }
        }
        public int WeekOfYear(DateTime date)
        {
            // use the current culture
            System.Globalization.CultureInfo ci = System.Threading.Thread.CurrentThread.CurrentCulture;
            // use current culture's calendar
            System.Globalization.Calendar cal = ci.Calendar;
            // get the calendar week rule (i.e. what determines the first week in the year)
            System.Globalization.CalendarWeekRule cwr = ci.DateTimeFormat.CalendarWeekRule;
            // get the first day of week for the current culture
            DayOfWeek fdow = ci.DateTimeFormat.FirstDayOfWeek;
            // return the week
            return cal.GetWeekOfYear(date, cwr, fdow);
        }
        public static DateTime MondayOfWeek(int week, int year)
        {
            string year1 = year.ToString();
            year1 += "-01-01";
            DateTime d1 = DateTime.Parse(year1);

            d1 = d1.AddDays(7 * week);
            if (d1.DayOfWeek == DayOfWeek.Sunday)
                return d1.AddDays(-6);

            int monday = d1.DayOfWeek - DayOfWeek.Monday;
            return d1.AddDays(-monday);
        }
        public int get_last_week(int id)
        {
            var last_week = (from p in new TblWeekController().FetchAll() orderby p.Id descending select p).ToList();
            int last = 0;
            for (int i = 0; i < last_week.Count; i++)
            {
                if (last_week[i].Id == id)
                {
                    last = last_week[i + 1].Id;
                    break;
                }
            }
            return last;
        }
        public int get_next_week(int id)
        {
            var next_week = (from p in new TblWeekController().FetchAll() orderby p.Id descending select p).ToList();
            int next = 0;
            if (next_week[0].Id != id)
            {
                for (int i = 1; i < next_week.Count; i++)
                {
                    if (next_week[i].Id == id)
                    {
                        next = next_week[i - 1].Id;
                        break;
                    }
                }
            }
            return next;
        }
    }   
}